﻿#include <QPainter>
#include <QPen>

#include "CPlcItemCoil.h"

CPlcItemCoil::CPlcItemCoil(const QRectF &rect,
                           const int8_t type,
                           const QString &str,
                           IPlcBaseItem *parent)
    : IPlcBaseItem(parent)
{
    mVariableNum  = str;
    mSelectActive = false;
    mSwitchOn     = false;
    mLastSwitchOn = false;
    mType         = type;
    setRect(rect);
}

CPlcItemCoil::~CPlcItemCoil()
{
    //如果当前为选中状态
    if (mSelectActive) setOnlySelectItem(NULL);
}

QRectF CPlcItemCoil::getRect()
{
    return mRect;
}

void CPlcItemCoil::setRect(const QRectF &rect)
{
    if (mRect == rect) return;
    //准备调整几何边界
    prepareGeometryChange();
    mRect         = rect;
    mBoundingRect = QRectF();
    // 缩放比例
    mdWidthScale  = mRect.width() / 65.0;
    mdheightScale = mRect.height() / 55.0;
    //重绘
    update();
}

QString CPlcItemCoil::getVariableNum()
{
    return mVariableNum;
}

void CPlcItemCoil::setVariableNum(const QString &str)
{
    if (mVariableNum == str) return;
    //重新设置变量编号
    mVariableNum = str;
    //重绘
    update();
}

void CPlcItemCoil::setSelectActive(bool status)
{
    //复位之前选中的图元
    if (this != getOnlySelectItem())
    {
        if (getOnlySelectItem() != NULL)
            getOnlySelectItem()->setSelectActive(false);
    }
    if (mSelectActive == status) return;

    mSelectActive = status;
    status ? setOnlySelectItem(this) : setOnlySelectItem(NULL);
    //重绘图元
    update();
}

void CPlcItemCoil::setLastSwitchOn(bool value)
{
    if (mLastSwitchOn == value) return;

    mLastSwitchOn = value;
    //重绘图元
    update();
}
void CPlcItemCoil::setSwitchOn(bool value)
{
    if (mSwitchOn == value) return;

    mSwitchOn = value;
    //重绘图元
    update();
}

int8_t CPlcItemCoil::getItemType()
{
    return mType;
}

void CPlcItemCoil::setItemType(int8_t type)
{
    if (mType == type) return;
    mType = type;
    update();
}

QRectF CPlcItemCoil::boundingRect() const
{
    if (mBoundingRect.isNull()) mBoundingRect = mRect;

    return mBoundingRect;
}

void CPlcItemCoil::paint(QPainter *painter,
                         const QStyleOptionGraphicsItem *option,
                         QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    // 设置反走样
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setRenderHint(QPainter::TextAntialiasing, true);

    QBrush blackBrush(Qt::black);
    QBrush blueBrush(QColor(2, 167, 240));
    QPen pen;
    //绘制选中状态
    if (mSelectActive)
    {
        //设置画笔样式为虚线
        pen.setStyle(Qt::DashLine);
        pen.setWidth(1);         //线宽1
        pen.setBrush(Qt::red);   //颜色蓝色

        painter->setPen(pen);
        painter->drawRect(mRect);   //画矩形
    }

    //绘制元件图标
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(0);            //线宽1
    pen.setBrush(blackBrush);   //颜色
    painter->setPen(pen);
    double x1 = mRect.left();                       //最左边导线点
    double x2 = mRect.left() + mdWidthScale * 24;   //元件左边点

    double y1 = mRect.top() + mdheightScale * 25;   //元件顶点
    double y2 = mRect.top() + mdheightScale * 35;   //连接线点

    if (mLastSwitchOn)
    {
        pen.setBrush(blueBrush);
        painter->setPen(pen);
    }
    else
    {
        pen.setBrush(blackBrush);
        painter->setPen(pen);
    }
    painter->drawLine(x1, y2, x2, y2);   //画直线

    //绘制信号接通状态
    if (mSwitchOn)
    {
        pen.setBrush(blueBrush);
        painter->setPen(pen);
        painter->setBrush(blueBrush);
    }
    else
    {
        pen.setBrush(blackBrush);
        painter->setPen(pen);
        painter->setBrush(Qt::NoBrush);
    }
    //画椭圆
    QRectF rect1(x2, y1, 30 * mdWidthScale, 20 * mdheightScale);
    painter->drawEllipse(rect1);

    //绘制元件类型
    pen.setBrush(blackBrush);
    painter->setPen(pen);
    switch (mType)
    {
    case plc::Coil:   //线圈
        break;
    case plc::Set:   //置位
        painter->drawText(rect1, Qt::AlignCenter, "S");
        break;
    case plc::Reset:   //复位
        painter->drawText(rect1, Qt::AlignCenter, "R");
        break;
    }

    //绘制变量编号
    QFont font;
    font.setPixelSize(13);   //字体像素大小
    painter->setFont(font);
    painter->drawText(QRectF(mRect.left() + 7.5 * mdWidthScale, mRect.top(),
                             50 * mdWidthScale, 25 * mdheightScale),
                      Qt::AlignCenter, mVariableNum);
}

bool CPlcItemCoil::getEditEnb()
{
    return true;
}

bool CPlcItemCoil::getSelectActiveEnb()
{
    return true;
}

bool CPlcItemCoil::getSelectActive()
{
    return mSelectActive;
}
